package player;

import java.util.ArrayList;

import thread.ThreadManager;
  
public class LoginManager 
{ 
	public static LoginManager getInstance()
	{
		if (instance == null)
		{
			synchronized(LoginManager.class)
			{
				if (instance == null)
				{
					instance = new LoginManager();
				}
			}
		}
		
		return instance;
	}
	
	public boolean initialize()
	{
		playerThreads = new ArrayList<PlayerThread>(3);
		
		for (int i = 0; i < 10; i++)
		{
			PlayerThread playerThread = new PlayerThread();
			playerThreads.add(playerThread);
			ThreadManager.getInstance().createThread(playerThread, true);
		}
		
		return true;
	}
	
	
	public boolean addPlayer(Player player) 
	{
		int runtimeID = player.getRuntimeID();
		int index = runtimeID % playerThreads.size();
		PlayerThread playerThread = playerThreads.get(index);
		
		return playerThread.addPlayer(player);
	}
	
	public boolean removePlayer(Player player) 
	{
		int runtimeID = player.getRuntimeID();
		int index = runtimeID % playerThreads.size();
		PlayerThread playerThread = playerThreads.get(index);
		
		return playerThread.removePlayer(player);
	}
	
	private static LoginManager instance = null;
	private ArrayList<PlayerThread> playerThreads; 
}
